package com.luoqian.demo03;

import java.lang.reflect.Constructor;

/**
 * 构造器的操作
 */
public class Constructor_Operation {

    public static void main(String[] args) throws Exception {
        Class<User> userClass = User.class;
        //获取所有公有的构造器 不包含父类的
        Constructor<?>[] constructors = userClass.getConstructors();
        for (Constructor c:constructors) {
            System.out.println(c.getModifiers() + "-" +c.getName());
        }
        System.out.println("==============");
        //获取所有的构造器
        Constructor<?>[] declaredConstructors = userClass.getDeclaredConstructors();
        for (Constructor c:declaredConstructors) {
            System.out.println(c.getModifiers() + "-" +c.getName());
        }

        // 1.直接通过newInstance创建对象

        //2.获取对应的Constructor对象获取实例
        Constructor<User> declaredConstructor = userClass.getDeclaredConstructor(String.class, String.class);
        declaredConstructor.setAccessible(true);
        User user = declaredConstructor.newInstance("jackk", "nan");
        System.out.println(user);
    }
}
